package com.example.test0.Controller;

import com.example.test0.mapper.GoodsMapper;
import com.example.test0.mapper.UserMapper;
import com.example.test0.model.Goods;
import com.example.test0.model.GoodsImfBean;
import com.example.test0.service.GoodsService;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class IndexController {

    @Autowired
    private GoodsService goodsService;

    //    @Autowired
//    private GoodsMapper goodsMapper;
    @PostMapping("/startup")
    @ResponseBody
    public String startUp(HttpServletRequest request) {
        String ret;
        request.getSession().setAttribute("user", "visitor");
        List<GoodsImfBean> goodsList = goodsService.FindRemGoods(1, "1001");
        //ret = new MainRecommend().InitRecommend(1,"visitor");
        ret = new Gson().toJson(goodsList);
        return ret;
    }

    @PostMapping("/index")
    @ResponseBody
    public String index(HttpServletRequest request) {
        String ret = null;

        HttpSession session = request.getSession();
        String ind_user = (String) session.getAttribute("user");
        String ind_username = (String) session.getAttribute("username");
        String ind_id = (String) session.getAttribute("userid");

        if (Objects.equals(ind_user, "admin")) {
            System.out.println("now user power is admin");
            List<GoodsImfBean> goodsList = goodsService.FindRemGoods(3, ind_id);
            ret = new Gson().toJson(goodsList);
        } else if (Objects.equals(ind_user, "user")) {
            System.out.println("now user power is user");
            List<GoodsImfBean> goodsList = goodsService.FindRemGoods(2, ind_id);
            ret = new Gson().toJson(goodsList);
        } else if (Objects.equals(ind_user, "visitor")) {
            System.out.println("now user power is visitor");
            List<GoodsImfBean> goodsList = goodsService.FindRemGoods(1, ind_id);
            ret = new Gson().toJson(goodsList);
        }

        return ret;
    }

}
